package com.suql.networktools

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences

@SuppressLint("StaticFieldLeak")
/**
 * Created by Calm on 2017/11/27.
 * Preference工具类
 */

object AppPreference {
    private var mSharedPreferences: SharedPreferences? = null
    val LOGIN_SIGN = "login.sign"
    val USER_NAME = "user.name"
    //存储bool值，true本地  false远程
    val AIOT_CONTROL_MODEL = "aiot.control.model"
    val LOGIN_COOKIE = "login.cookie"
    val IP_ADDRESS = "ip.address"
    private var mContext: Context? = null
    fun initSharedPreferences(context: Context) {
        mContext = context
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(NetWorkConstants.APPPREFERENCE_FILE_NAME, Context.MODE_PRIVATE)
        }
    }

    fun getStringWithId(id: Int): String {
        return mContext!!.getString(id)
    }

    fun putInt(tag: String, vaule: Int) {
        mSharedPreferences!!.edit().putInt(tag, vaule).apply()
    }

    fun getInt(tag: String, defVaule: Int): Int {
        return mSharedPreferences!!.getInt(tag, defVaule)
    }

    fun putString(tag: String, vaule: String) {
        mSharedPreferences!!.edit().putString(tag, vaule).apply()
    }

    fun getString(tag: String, defVaule: String): String? {
        return mSharedPreferences!!.getString(tag, defVaule)
    }

    fun putBoolean(tag: String, b: Boolean) {
        mSharedPreferences!!.edit().putBoolean(tag, b).apply()
    }

    fun getBoolean(tag: String, defVaule: Boolean): Boolean {
        return mSharedPreferences!!.getBoolean(tag, defVaule)
    }
}
